#include <stdio.h>
#include <string.h>
#include <stdint.h>
#include "aes.h"

int test_aes(void)
{
	printf("\nTesting AES128\n\n");
	
	char key[16] = "0123456789abcdef";	// 密钥
	char iv[16] = "0102030405123456";	// 初始向量
	char buff[64] = "This is a tinyAES Demo, using CBC Mode!";	// 待加密数据
	
	struct AES_ctx ctx;
	
	// 加密
	char origin[64];
	strcpy(origin, buff);	// 原始数据副本
	AES_init_ctx_iv(&ctx, (uint8_t *)key, (uint8_t *)iv);
	AES_CBC_encrypt_buffer(&ctx, (uint8_t *)buff, sizeof(buff));	// 结果存放在buff数组
	
	// 解密
	AES_init_ctx_iv(&ctx, (uint8_t *)key, (uint8_t *)iv);
	AES_CBC_decrypt_buffer(&ctx, (uint8_t *)buff, sizeof(buff));	// 结果存放在decode数组
	
	printf("CBC encrypt: %s\n", origin);
	printf("CBC decrypt: %s\n", buff);

	// 数据比对
	if (0 == memcmp(origin, buff, strlen(buff))) {
		printf("\nTest result: SUCCESS!\n");
		return(0);
	}
	printf("\nTest result: FAILURE!\n");
	return(1);
}
